Product
Introducing Socket Optimize
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.
remark-stringify
Advanced tools
The remark-stringify package is a plugin for the remark ecosystem that allows you to serialize Markdown Abstract Syntax Trees (ASTs) into Markdown text. It is commonly used in conjunction with other remark plugins to process and transform Markdown content programmatically.
Basic Markdown Serialization
This feature allows you to convert a Markdown AST into a Markdown string. The example demonstrates how to serialize a simple AST representing a paragraph with the text 'Hello, world!'.
const remark = require('remark');
const stringify = require('remark-stringify');
const markdownAST = {
type: 'root',
children: [
{ type: 'paragraph', children: [{ type: 'text', value: 'Hello, world!' }] }
]
};
const markdownText = remark().use(stringify).stringify(markdownAST);
console.log(markdownText); // Outputs: 'Hello, world!\n'
Customizing Output
This feature allows you to customize the output format of the serialized Markdown. The example shows how to set options for bullet points and fenced code blocks.
const remark = require('remark');
const stringify = require('remark-stringify');
const markdownAST = {
type: 'root',
children: [
{ type: 'paragraph', children: [{ type: 'text', value: 'Hello, world!' }] }
]
};
const markdownText = remark()
.use(stringify, { bullet: '*', fences: true })
.stringify(markdownAST);
console.log(markdownText); // Outputs: 'Hello, world!\n'
Handling Complex ASTs
This feature demonstrates how to handle more complex ASTs, including headings, paragraphs, and lists. The example shows how to serialize an AST with multiple types of nodes.
const remark = require('remark');
const stringify = require('remark-stringify');
const markdownAST = {
type: 'root',
children: [
{ type: 'heading', depth: 1, children: [{ type: 'text', value: 'Title' }] },
{ type: 'paragraph', children: [{ type: 'text', value: 'This is a paragraph.' }] },
{ type: 'list', ordered: false, children: [
{ type: 'listItem', children: [{ type: 'paragraph', children: [{ type: 'text', value: 'Item 1' }] }] },
{ type: 'listItem', children: [{ type: 'paragraph', children: [{ type: 'text', value: 'Item 2' }] }] }
] }
]
};
const markdownText = remark().use(stringify).stringify(markdownAST);
console.log(markdownText); // Outputs: '# Title\n\nThis is a paragraph.\n\n* Item 1\n* Item 2\n'
Markdown-it is a fast and flexible Markdown parser that can also be used to render Markdown from an AST. It offers a wide range of plugins and customization options, making it a versatile alternative to remark-stringify.
Marked is a low-level Markdown compiler that allows for extensive customization. It is known for its speed and flexibility, making it a good choice for projects that require fine-grained control over Markdown rendering.
Showdown is a bidirectional Markdown to HTML converter that can also be used to serialize Markdown from an AST. It is easy to use and offers a range of extensions for additional functionality.
Compiler for unified. Serializes mdast syntax trees to Markdown. Used in the remark processor but can be used on its own as well. Can be extended to change how Markdown is serialized.
Gatsby 🥇 |
Vercel 🥇 |
Netlify | ||
Holloway |
ThemeIsle 🥉 |
BoostIO 🥉 |
Expo 🥉 |
You? |
npm:
npm install remark-stringify
var unified = require('unified')
var createStream = require('unified-stream')
var html = require('rehype-parse')
var rehype2remark = require('rehype-remark')
var stringify = require('remark-stringify')
var processor = unified()
.use(html)
.use(rehype2remark)
.use(stringify, {
bullet: '*',
fence: '~',
fences: true,
incrementListMarker: false
})
process.stdin.pipe(createStream(processor)).pipe(process.stdout)
See unified for more examples »
processor().use(stringify[, options])
Configure the processor
to serialize mdast syntax trees to
Markdown.
options
Options can be passed directly, or passed later through
processor.data()
.
options.gfm
Serialize with the required escapes for GFM compatible Markdown (boolean
,
default: true
).
|
, for tables):
, for literal URLs)~
, for strike-through)options.commonmark
Serialize for CommonMark compatible Markdown (boolean
, default: false
).
options.pedantic
⚠️ Pedantic was previously used to mimic old-style Markdown mode: no tables, no fenced code, and with many bugs. It’s currently still “working”, but please do not use it, it’ll be removed in the future.
options.entities
⚠️ entities
was previously used, but included bugs.
It’s currently still “working”, but please do not use it, it’ll be removed in
the future.
options.setext
Serialize headings, when possible, in Setext-style (boolean
, default: false
).
Uses =
for level one headings and -
for level two headings.
Other heading levels are serialized as ATX (respecting closeAtx
).
options.closeAtx
Serialize ATX headings with the same amount of closing hashes as opening hashes
(boolean
, default: false
).
options.tableCellPadding
Create tables with a space between cell delimiters (|
) and content (boolean
,
default: true
).
options.tablePipeAlign
Align the delimiters (|
) between table cells so that they all align nicely and
form a grid (boolean
, default: true
).
options.stringLength
Function passed to markdown-table
to detect the length of a
table cell (Function
, default: s => s.length
).
Used to pad tables.
options.fence
Marker to use for fenced code blocks ('~'
or '`'
, default: '`'
).
options.fences
Create code blocks with a fence instead of indentation if they have no info
string (boolean
, default: false
).
When false
, code blocks are indented.
Code blocks with an info string are always fenced.
options.bullet
Marker to use for the bullet of unordered list items ('-'
, '*'
, or '+'
,
default: '-'
).
options.listItemIndent
Style of indentation for list items ('tab'
, 'mixed'
or '1'
, default:
'tab'
).
'tab'
: use a tab stops (4 spaces)'1'
: use one space'mixed'
: use 1
for tight and tab
for loose list itemsoptions.incrementListMarker
Increment ordered list item numbers (boolean
, default: true
).
When false
, all list item numbers will be the same.
options.tightDefinitions
Separate definitions with a single line feed (boolean
, default: false
).
When false
, definitions will have blank lines between them, similar to other
blocks.
options.rule
Marker to use for thematic breaks / horizontal rules ('-'
, '*'
, or '_'
,
default: '*'
).
options.ruleRepetition
Number of markers to use for thematic breaks / horizontal rules (number
,
default: 3
).
Musts be 3
or more.
options.ruleSpaces
Place a space between thematic break (horizontal rule) markers (boolean
,
default true
).
options.strong
Marker to use for importance ('_'
or '*'
, default '*'
).
options.emphasis
Marker to use for emphasis ('_'
or '*'
, default '_'
).
stringify.Compiler
Access to the compiler, if you need it.
Compiler
If the remark-stringify
plugin is used, it adds a Compiler
constructor function to the processor
.
Other plugins can add visitors to its prototype to change how Markdown is
serialized.
The below plugin modifies a visitor to add an extra blank line before
headings with a rank of 2
.
module.exports = gap
function gap() {
var Compiler = this.Compiler
var visitors = Compiler.prototype.visitors
var original = visitors.heading
visitors.heading = heading
function heading(node) {
return (node.depth === 2 ? '\n' : '') + original.apply(this, arguments)
}
}
Compiler#visitors
Map of types to visitors (Object.<Function>
).
function visitor(node[, parent])
Serialize node
.
node
(Node
) — Node to compileparent
(Parent
, optional) — Parent of node
.
Not available on the root nodestring
— Serialized given node
.
As Markdown is sometimes used for HTML, and improper use of HTML can open you up
to a cross-site scripting (XSS) attack, use of remark can also be unsafe.
When going to HTML, use remark in combination with the rehype
ecosystem, and use rehype-sanitize
to make the tree safe.
Use of remark plugins could also open you up to other attacks. Carefully assess each plugin and the risks involved in using them.
See contributing.md
in remarkjs/.github
for ways
to get started.
See support.md
for ways to get help.
Ideas for new plugins and tools can be posted in remarkjs/ideas
.
A curated list of awesome remark resources can be found in awesome remark.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.
FAQs
remark plugin to add support for serializing markdown
The npm package remark-stringify receives a total of 3,652,259 weekly downloads. As such, remark-stringify popularity was classified as popular.
We found that remark-stringify demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
We're excited to introduce Socket Optimize, a powerful CLI command to secure open source dependencies with tested, optimized package overrides.
Product
We're excited to announce that Socket now supports the Java programming language.
Security News
Socket detected a malicious Python package impersonating a popular browser cookie library to steal passwords, screenshots, webcam images, and Discord tokens.